package com.gr.smartedubackend.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gr.smartedubackend.common.BusinessException;
import com.gr.smartedubackend.common.ErrorCode;
import com.gr.smartedubackend.constant.CommonConstant;
import com.gr.smartedubackend.model.dto.teacher.TeacherQueryRequest;
import com.gr.smartedubackend.model.entity.Teacher;
import com.gr.smartedubackend.service.TeacherService;
import com.gr.smartedubackend.mapper.TeacherMapper;
import org.springframework.stereotype.Service;

/**
 * @author 尘轩
 * @description 针对表【teacher】的数据库操作Service实现
 * @createDate 2025-06-30 20:25:45
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
        implements TeacherService {

    @Override
    public QueryWrapper<Teacher> getQueryWrapper(TeacherQueryRequest teacherQueryRequest) {
        if (teacherQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }

        Long id = teacherQueryRequest.getId();
        String teacherName = teacherQueryRequest.getTeacherName();
        String teacherNumber = teacherQueryRequest.getTeacherNumber();
        String subject = teacherQueryRequest.getSubject();
        String phone = teacherQueryRequest.getPhone();
        String schoolName = teacherQueryRequest.getSchoolName();
        String introduction = teacherQueryRequest.getIntroduction();
        String sortField = teacherQueryRequest.getSortField();
        String sortOrder = teacherQueryRequest.getSortOrder();
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotEmpty(id), "id", id);
        queryWrapper.like(StrUtil.isNotEmpty(teacherName), "teacherName", teacherName);
        queryWrapper.like(StrUtil.isNotEmpty(teacherNumber), "teacherNumber", teacherNumber);
        queryWrapper.like(StrUtil.isNotEmpty(subject), "subject", subject);
        queryWrapper.like(StrUtil.isNotEmpty(phone), "phone", phone);
        queryWrapper.like(StrUtil.isNotEmpty(schoolName), "schoolName", schoolName);
        queryWrapper.like(StrUtil.isNotEmpty(introduction), "introduction", introduction);
        if (StrUtil.isNotEmpty(sortOrder) && StrUtil.isNotEmpty(sortField)) {
            queryWrapper.orderBy(true, sortOrder.equals("ascend"), sortField);
        } else {
            queryWrapper.orderByDesc(CommonConstant.CREATE_TIME);
        }

        return queryWrapper;
    }
}




